﻿using HANS_BackStageWeb.Common;
using System.Web.Mvc;
using HANS_WeChat.Base.Model.DbModel;
using HANS_Base.Model;
using HANS_Base.Services;
using HANS.DataBaseTool;
using HANS.Utility.Extensions;
using HANS.WeChat;
using HANS_WeChat.Base.Model.WeModel.CommonMenu;
using System.Linq;
using MvcPaging;

namespace HANS_BackStageWeb.Areas.HANSWeChat.Controllers
{
    public class WeChatMenuController : CommonController
    {
        // GET: HANSWeChat/WeChatMenu
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Test() {

            return View();
        }


        /// <summary>
        /// 菜单列表
        /// </summary>
        /// <returns></returns>
        public ActionResult CommonMenuList() {

            return View(WeChatMenuService.GetCommonMenuList());
        }


        [HttpGet]
        public ActionResult CreateCommonMenu() {

            ViewData["mainMenu"] = WeChatMenuService.GetCommonMainMenuList();
            return View(new Commonmenu());
        }

        /// <summary>
        /// 创建菜单
        /// </summary>
        /// <param name="commonmenu"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CreateCommonMenu(Commonmenu commonmenu) {
            JsonResultData result = new JsonResultData();
            result.RunWithTry(item =>
            {
                ModelState.Remove("Id");
                ModelState.Remove("Wechat");
                ModelState.Remove("Menutype");
                ModelState.Remove("Menukey");
                string guid = "";
                commonmenu.Id = guid.NewGuid();
                commonmenu.Wechat = WeChatBaseInfo.AppID;
                if (ModelState.IsValid)
                {
                    Msg msg = new Msg();
                    msg = WeChatMenuService.InsertCommonMenu(commonmenu);
                    if (msg.Result)
                    {
                        result.AddSuccessMessage(msg.ToString());
                        result.RedirectUrl = Url.Action("CommonMenuList", "WeChatMenu", new { area = "HANSWeChat" });
                    }
                    else
                    {
                        result.Success = false;
                        result.ReloadPage = true;
                        result.AddMessage(msg.ToString());
                    }

                }
                else
                {
                    result.AddModelValidErrorMessage(ModelState);
                }
            });
            return JsonAllowGet(result);
        }



        [HttpGet]
        public ActionResult UpdateCommonMenu(string id) {
            ViewData["mainMenu"] = WeChatMenuService.GetCommonMainMenuList();
            return View(WeChatMenuService.LoadCommonmenu(id));
        }

        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="commonmenu"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateCommonMenu(Commonmenu commonmenu) {
            JsonResultData result = new JsonResultData();
            result.RunWithTry(item =>
            {
                ModelState.Remove("Id");
                ModelState.Remove("Wechat");
                ModelState.Remove("Menutype");
                ModelState.Remove("Menukey");
                if (ModelState.IsValid)
                {
                    var _CommonMenu = WeChatMenuService.LoadCommonmenu(commonmenu.Id);
                    if (_CommonMenu.IsNotNull())
                    {
                        Msg msg = new Msg();
                        _CommonMenu.Menuname = commonmenu.Menuname;
                        _CommonMenu.Menukey = commonmenu.Menukey;
                        _CommonMenu.Menutype = commonmenu.Menutype;
                        _CommonMenu.Parent_id = commonmenu.Parent_id;
                        _CommonMenu.Menuorder = commonmenu.Menuorder;
                        _CommonMenu.Url = commonmenu.Url;
                        _CommonMenu.Media_id = commonmenu.Media_id;
                        _CommonMenu.Appid = commonmenu.Appid;
                        _CommonMenu.Pagepath = commonmenu.Pagepath;
                        msg = WeChatMenuService.UpdateCommonMenu(_CommonMenu);
                        if (msg.Result)
                        {
                            result.AddSuccessMessage(msg.ToString());
                            result.RedirectUrl = Url.Action("CommonMenuList", "WeChatMenu", new { area = "HANSWeChat" });
                        }
                        else
                        {
                            result.Success = false;
                            result.ReloadPage = true;
                            result.AddMessage(msg.ToString());
                        }
                    }
                    else
                    {
                        result.AddMessage("菜单不存在！");
                        result.Success = false;
                        result.RedirectUrl = Url.Action("CommonMenuList", "WeChatMenu", new { area = "HANSWeChat" });
                    }


                }
                else
                {
                    result.AddModelValidErrorMessage(ModelState);
                }
            });
            return JsonAllowGet(result);
        }


        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult DeleteCommonMenu(string id) {
            JsonResultData result = new JsonResultData();
            result.RunWithTry(item =>
            {
                Msg msg = new Msg();
                msg = WeChatMenuService.DeleteCommonMenu(id);
                result.Success = msg.Result;
                result.AddMessage(msg.ToString());
                result.ReloadPage = true;
            });
            return JsonAllowGet(result);
        }


        public JsonResult CommonMenuCloudDownload() {

            JsonResultData result = new JsonResultData();
            result.RunWithTry(item =>
            {
                Msg msg = new Msg();
                msg = WeChatMenuService.CommonMenuCloudDownload();
                result.Success = msg.Result;
                result.AddMessage(msg.ToString());
                result.ReloadPage = true;
            });
            return JsonAllowGet(result);
        }

        public JsonResult CommonMenuCloudUpload() {
            JsonResultData result = new JsonResultData();
            result.RunWithTry(item =>
            {
                Msg msg = new Msg();
                msg = WeChatMenuService.CommonMenuCloudUpload();
                result.Success = msg.Result;
                result.AddMessage(msg.ToString());
                result.ReloadPage = true;
            });
            return JsonAllowGet(result);
        }


    }
}